package cn.edu.sdju.fengguoyu.ahoedgemonitor;

import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.net.InetSocketAddress;

public class DialogBuilder {
    public static Dialog<InetSocketAddress> newSocketAddressDialog() {
        Dialog<InetSocketAddress> dialog = new Dialog<>();
        dialog.setTitle("新建连接");
        dialog.setHeaderText("创建与注册服务器的连接");
        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.APPLY);
        VBox vBox = new VBox();
        TextField address = new TextField();
        TextField port = new TextField();
        port.setText("50051");
        HBox hBox = new HBox();
        Label label = new Label(" : ");
        label.setFont(new Font(18.0));
        port.setPrefWidth(50);
        hBox.getChildren().addAll(address, label,port);
        vBox.getChildren().addAll(new Label("请输入服务器地址:"), hBox);
        dialog.getDialogPane().setContent(vBox);
        dialog.setResultConverter((ButtonType buttonType) -> {
            if (buttonType == ButtonType.APPLY) {
                return new InetSocketAddress(address.getText(),Integer.parseInt(port.getText()));
            }
            return null;
        });
        return dialog;
    }
}
